//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BMASwapRateHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/BMAIndex.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Yield/RateHelper.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::CBMASwapRateHelper (Cephei::QL::IQuote^ liborFraction, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ bmaPeriod, QL::Times::BusinessDayConventionEnum bmaConvention, Cephei::QL::Times::IDayCounter^ bmaDayCount, Cephei::QL::Indexes::IBMAIndex^ bmaIndex, Cephei::QL::Indexes::IIborIndex^ index) : CRateHelper(CBMASwapRateHelper::typeid)
{
    CQuote^ _CliborFraction;
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CPeriod^ _CbmaPeriod;
    CDayCounter^ _CbmaDayCount;
    CBMAIndex^ _CbmaIndex;
    CIborIndex^ _Cindex;
    try
    {
#ifdef HANDLE
        _phBMASwapRateHelper = NULL;
#endif
        _CliborFraction = safe_cast<CQuote^> (liborFraction);
        _CliborFraction->Lock();
        Handle<QuantLib::Quote>& _liborFraction = static_cast<Handle<QuantLib::Quote>&> (_CliborFraction->GetHandle ()); 
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        _CbmaPeriod = safe_cast<CPeriod^> (bmaPeriod);
        _CbmaPeriod->Lock();
        QuantLib::Period& _bmaPeriod = static_cast<QuantLib::Period&> (_CbmaPeriod->GetReference ()); 
        QuantLib::BusinessDayConvention _bmaConvention = (QuantLib::BusinessDayConvention)bmaConvention ;
        _CbmaDayCount = safe_cast<CDayCounter^> (bmaDayCount);
        _CbmaDayCount->Lock();
        QuantLib::DayCounter& _bmaDayCount = static_cast<QuantLib::DayCounter&> (_CbmaDayCount->GetReference ()); 
        _CbmaIndex = safe_cast<CBMAIndex^> (bmaIndex);
        _CbmaIndex->Lock();
        boost::shared_ptr<QuantLib::BMAIndex>& _bmaIndex = static_cast<boost::shared_ptr<QuantLib::BMAIndex>&> (_CbmaIndex->GetShared ()); 
        _Cindex = safe_cast<CIborIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _index = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_Cindex->GetShared ()); 
        _ppBMASwapRateHelper = new boost::shared_ptr<QuantLib::BMASwapRateHelper> (new QuantLib::BMASwapRateHelper ( _liborFraction,  _tenor,  _settlementDays,  _calendar,  _bmaPeriod,  _bmaConvention,  _bmaDayCount,  _bmaIndex,  _index ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBMASwapRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CliborFraction != nullptr) _CliborFraction->Unlock();
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CbmaPeriod != nullptr) _CbmaPeriod->Unlock();
        if (_CbmaDayCount != nullptr) _CbmaDayCount->Unlock();
        if (_CbmaIndex != nullptr) _CbmaIndex->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::CBMASwapRateHelper (boost::shared_ptr<QuantLib::BMASwapRateHelper>& childNative, Object^ owner) : CRateHelper(CBMASwapRateHelper::typeid)
{
#ifdef HANDLE
	_phBMASwapRateHelper = NULL;
#endif
	_ppBMASwapRateHelper = &childNative;
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBMASwapRateHelper));
}
Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::CBMASwapRateHelper (QuantLib::BMASwapRateHelper& childNative, Object^ owner) : CRateHelper(CBMASwapRateHelper::typeid)
{
#ifdef HANDLE
	_phBMASwapRateHelper = NULL;
#endif
	_ppBMASwapRateHelper = new boost::shared_ptr<QuantLib::BMASwapRateHelper> (&childNative);
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBMASwapRateHelper));
    _BMASwapRateHelperOwner = owner;
    _RateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::CBMASwapRateHelper (CBMASwapRateHelper^ copy) : CRateHelper(CBMASwapRateHelper::typeid)
{
#ifdef HANDLE
	_phBMASwapRateHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBMASwapRateHelper = new boost::shared_ptr<QuantLib::BMASwapRateHelper> (copy->GetShared());
        _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBMASwapRateHelper));
    }
}
Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::CBMASwapRateHelper (PLATFORM::Type^ t) : CRateHelper(CBMASwapRateHelper::typeid)
{
#ifdef HANDLE
	_phBMASwapRateHelper = NULL;
#endif
	if (!t->IsSubclassOf(CBMASwapRateHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::CBMASwapRateHelper (QuantLib::Handle<QuantLib::BMASwapRateHelper>& childNative, Object^ owner)  : CRateHelper(CBMASwapRateHelper::typeid)
{
	_phBMASwapRateHelper = &childNative;
	_ppBMASwapRateHelper = &static_cast<boost::shared_ptr<QuantLib::BMASwapRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBMASwapRateHelper));
    _BMASwapRateHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::CBMASwapRateHelper (QuantLib::Handle<QuantLib::BMASwapRateHelper> childNative)  : CRateHelper(CBMASwapRateHelper::typeid)
{
	_phBMASwapRateHelper = &childNative;
	_ppBMASwapRateHelper = &static_cast<boost::shared_ptr<QuantLib::BMASwapRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBMASwapRateHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::CBMASwapRateHelper (QuantLib::BMASwapRateHelper childNative)  : CRateHelper(CBMASwapRateHelper::typeid)
{
#ifdef HANDLE
	_phBMASwapRateHelper = NULL;
#endif
	_ppBMASwapRateHelper = new boost::shared_ptr<QuantLib::BMASwapRateHelper> (new QuantLib::BMASwapRateHelper (childNative));
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBMASwapRateHelper));
}
#endif

Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::~CBMASwapRateHelper ()
{
    if (_ppBMASwapRateHelper != NULL)
    {
	    delete _ppBMASwapRateHelper;
        _ppBMASwapRateHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::!CBMASwapRateHelper ()
{
    if (_ppBMASwapRateHelper != NULL)
    {
	    delete _ppBMASwapRateHelper;
    }
}
QuantLib::BMASwapRateHelper& Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::GetReference ()
{
    if (_ppBMASwapRateHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppBMASwapRateHelper;
}
boost::shared_ptr<QuantLib::BMASwapRateHelper>& Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::GetShared ()
{
    if (_ppBMASwapRateHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppBMASwapRateHelper;
}
QuantLib::BMASwapRateHelper* Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::GetPointer ()
{
    if (_ppBMASwapRateHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppBMASwapRateHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BMASwapRateHelper>& Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::GetHandle ()
{
	if (_phBMASwapRateHelper == NULL)
	{
		_phBMASwapRateHelper = new Handle<QuantLib::BMASwapRateHelper> (*_ppBMASwapRateHelper);
	}
	return *_phBMASwapRateHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::HasNative () 
{
	return (_ppBMASwapRateHelper != NULL);
}

Double Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBMASwapRateHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::IBMASwapRateHelper^ Cephei::QL::Termstructures::Yield::CBMASwapRateHelper::SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t)
{
    CYieldTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYieldTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YieldTermStructure* _t = static_cast<QuantLib::YieldTermStructure*> (_Ct->GetPointer ()); 
    	(*_ppBMASwapRateHelper)->setTermStructure ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IBMASwapRateHelper^ Cephei::QL::Termstructures::Yield::CBMASwapRateHelper_Factory::Create (Cephei::QL::IQuote^ liborFraction, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ bmaPeriod, QL::Times::BusinessDayConventionEnum bmaConvention, Cephei::QL::Times::IDayCounter^ bmaDayCount, Cephei::QL::Indexes::IBMAIndex^ bmaIndex, Cephei::QL::Indexes::IIborIndex^ index)
{
    return REFNEW CBMASwapRateHelper ( liborFraction,  tenor,  settlementDays,  calendar,  bmaPeriod,  bmaConvention,  bmaDayCount,  bmaIndex,  index);
}
